﻿using System;
using System.Web;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfConfirmReceipt : msBsc.Cls.BPCustom
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowData(this.Request.QueryString.Get("ID"));
            }
        }

        //private string strOrderIds="";
        //public DataRow drOrder = null;
        //private string GetOrderCountDesc()
        //{
        //    if (int.Parse(drOrder["ORDRE_COUNT"].ToString()) > 1)
        //    {
        //        return "合并｜" + drOrder["ORDRE_COUNT"].ToString() + "笔订单";
        //    }
        //    else
        //    {
        //        return "单笔订单";
        //    }
        //}
        private void ShowData(string strOrderIds)
        {
            if (string.IsNullOrEmpty(strOrderIds))
            {
                this.ToErrPage("当前没有找到需要确认收货的订单，请返回重新选择！");
            }
            using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objgoods = new AppLogic.DataAlc.CBizShpOrderForm())
            {
                strOrderIds = strOrderIds.Replace("'", "");
                DataSet dsList = objgoods.DataObj.QueryBySQL(@"
    SELECT 
    PAID_AMT=ISNULL(SUM(A.PAID_AMT),0)
    ,PAYABLE_AMT=ISNULL(SUM(A.PAYABLE_AMT),0)
    ,PAID_SCORE_QTY=ISNULL(SUM(A.PAID_SCORE_QTY),0)
    ,PAID_SCORE_AMT=ISNULL(SUM(A.PAID_SCORE_AMT),0)
    ,ORDRE_COUNT=COUNT(*)
    ,TRADING_NO=dbo.strJoin(A.ORDER_FORM_NO)
    ,TRADING_NME=dbo.strJoin(A.ORDER_FORM_NME) 
    FROM SHP_ORDER_FORM A 
    WHERE TRADE_STATUS_TXT='卖家已发货' AND A.USER_NME='" + msBsc.SvRef.UserInfo.Operator + @"' AND A.ORDER_FORM_ID IN ('" + strOrderIds.Replace(",", "','") + @"')
    ");
                if (dsList.Tables[0].Rows.Count <= 0)
                {
                    this.ToErrPage("当前没有找到需要确认收货的订单，可能已经做过收货确认，请返回重新选择！");
                    return;
                }
                DataRow drOrder = dsList.Tables[0].Rows[0];
                if (int.Parse(drOrder["ORDRE_COUNT"].ToString()) < 1)
                {
                    this.ToErrPage("当前没有找到需要确认收货的订单，可能已经做过收货确认，请返回重新选择！");
                    return;
                }

                this.ExecJScript("$('#orderId').val('" + strOrderIds + "');");
                string strtem1 = "";
                if (int.Parse(drOrder["ORDRE_COUNT"].ToString()) > 1)
                {
                    strtem1 = "合并｜" + drOrder["ORDRE_COUNT"].ToString() + "笔订单";
                }
                else
                {
                    strtem1 = "单笔订单";
                }
                this.ExecJScript("$('#divOrderCountDesc').html('" + strtem1 + "');");
                this.ExecJScript("$('#emPayAMt').html('" + drOrder["PAYABLE_AMT"].ToString() + "');");

                this.ExecJScript("$('#spnScoreAmt').html('" + drOrder["PAID_SCORE_AMT"].ToString() + "');");
                this.ExecJScript("$('#spnAlipayAmt').html('" + drOrder["PAID_AMT"].ToString() + "');");
                if ((decimal)drOrder["PAID_SCORE_AMT"] > 0M)
                {
                    this.ExecJScript("$('#divScorePay').show();");
                }
                if ((decimal)drOrder["PAID_AMT"] > 0M)
                {
                    this.ExecJScript("$('#divAliPay').show();");
                }


                this.ExecJScript("$('#spnOrderNos').html('" + drOrder["TRADING_NO"].ToString() + "');");
                this.ExecJScript("$('#spnOrderNmes').html('" + drOrder["TRADING_NME"].ToString() + "');");

                //if (msBsc.SvRef.UserInfo.OperType == "1")
                //{
                //    using (msBsc.AppLogic.DataAlc.CBizShpCommercialTenantInfo ouinfo = new AppLogic.DataAlc.CBizShpCommercialTenantInfo())
                //    {
                //        decimal decUserAmt = ouinfo.GetShopChargeAmt(msBsc.SvRef.UserInfo.Operator);
                //        this.ExecJScript("$('#spnUserAmt').html('" + decUserAmt + "');");
                //    }
                //}
                //else
                //{
                //    using (msBsc.AppLogic.DataAlc.CBizShpCustomerInfo ouinfo = new AppLogic.DataAlc.CBizShpCustomerInfo())
                //    {
                //        decimal decUserAmt = ouinfo.GetUserChargeAmt(msBsc.SvRef.UserInfo.Operator);
                //        this.ExecJScript("$('#spnUserAmt').html('" + decUserAmt + "');");
                //    }
                //}


            }
        }

        private void doConfirmPay()
        { 
            System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
            transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
            transactionOption.Timeout = new TimeSpan(0, 10, 0);
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
            {
                using (msBsc.AppLogic.DataAlc.CBizShpOrderForm objgoods = new AppLogic.DataAlc.CBizShpOrderForm())
                {
                    DataSet ds = objgoods.SelectByIds(this.GetParamHtValue("ID"));
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"].ToString() == "卖家已发货")
                        {
                            ds.Tables[0].Rows[i]["TRADE_STATUS_TXT"] = "交易成功";
                            ds.Tables[0].Rows[i]["AFFIRM_DT"] = DateTime.Now;
                        }
                    }
                    objgoods.Update(ds);
                    //using (msBsc.AppLogic.DataAlc.CBizSysUser ouser = new AppLogic.DataAlc.CBizSysUser())
                    //{
                    //    ouser.UpdateOrderStatistic(msBsc.SvRef.UserInfo.Operator);
                    //}
                    this.ExecJScript("layer.msg('成功确认收货！',1,{type:1,shade:false});");
                }
                ts.Complete();
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "doUpateAmt")
            {
                ShowData(this.GetParamHtValue("ID"));
            }
            else if (this.strAction == "doConfirmPay")
            {
                doConfirmPay();
            }
        }
         
    }
}